const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
		page: {
      type: String,
			value:""
    },
		area: {
			type: String,
			value:""
		},
		childCategory: {
			type: String,
			value:""
		},
		orderBy: {
			type: String,
			value:""
		},
		parentCategory: {
			type: String,
			value:""
		},
		searchContent: {
			type: String,
			value:""
		},
		type: {
			type: String,
			value:""
		},
		source:{
			type: String,
			value:""
		},
		status:{
			type: String,
			value:"2"
		},
		refresh:{
			type:Boolean
		}
  },
	ready: function() {
		this.queryArticle();
	},
  /**
   * 组件的初始数据
   */
  data: {
		dynamic:[],
		noneData:false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
		/* 查询最新动态 */
		queryArticle:function(){
			var _this=this;
			if(this.data.source=='searchdynamic'){
				var arr=[];
				var str="";
				var strs="";
			}
			wx.showLoading({
				title: '数据加载中...',
				mask:true
			})
			wx.request({
				url: util.url+"/article/queryArticle",
				data: {
					"area":_this.data.area,
					"childCategory":_this.data.childCategory,
					"page":_this.data.page,
					"size":"20",
					"status":_this.data.status,
					"orderBy":_this.data.orderBy,
					"parentCategory":_this.data.parentCategory,
					"searchContent":_this.data.searchContent,
					"type":_this.data.type
				},
				method:"post",
				header: {
					'content-type': 'application/json' 
				},
				success(res) {
					wx.hideLoading();
					if(res.data.resultCode==0){
						if(_this.data.refresh){
							wx.hideNavigationBarLoading() //完成停止加载
							wx.stopPullDownRefresh() //停止下拉刷新
						}
						
						var articleList=res.data.resultData.articleList;
						if(!articleList){
							articleList=[];
						}
						wx.setStorageSync('maxPage',Math.ceil(res.data.resultData.totalCount/20));
						wx.setStorageSync('newPage',true);
						if(res.data.resultData.totalCount==0){
							_this.setData({
								noneData:true,
							})
						}
						for(var i=0;i<articleList.length;i++){
							if(!articleList[i].coverUrl){
								articleList[i].coverUrl="";
								articleList[i].addClass="all-width";
							}
							articleList[i].publishAt=articleList[i].publishAt?util.formatTime(new Date(articleList[i].publishAt)):"";
							articleList[i].fileDate=articleList[i].fileDate?util.formatTime(new Date(articleList[i].fileDate)):"";
							if(_this.data.source=='searchdynamic'){
								strs=articleList[i].title;
								arr=strs.split(_this.data.searchContent);
								str="<span style='color:red;'>"+_this.data.searchContent+"</span>";
								articleList[i].title=arr.join(str);
								arr=[];
							}
						}
						if(articleList.length){
							_this.setData({
								dynamic:articleList,
							})
						}
						_this.data.refresh=false;
					}else{
						wx.showToast({
							icon:"none",
							title:res.data.resultMsg
						})
					}
					
				},
				fail: res => {
					wx.hideLoading();
					wx.showToast({
						title:"获取数据失败"
					})
				}
			})
		},
		/* 跳转到详情页 */
		toDetails:function(e){
			if(e.currentTarget.dataset.type==11){
				wx.navigateTo({
					url: "../articleDetail/articleDetail?judgeType=unscramble&id="+e.currentTarget.dataset.id
				})
			}else if(e.currentTarget.dataset.type==12){
				wx.navigateTo({
					url: "../articleDetail/articleDetail?judgeType=&id="+e.currentTarget.dataset.id+"&case=caseDetail"
				})
			}else if(e.currentTarget.dataset.type==13){
				wx.navigateTo({
					url: "../articleDetail/articleDetail?judgeType=manuscript&id="+e.currentTarget.dataset.id+"&parentId="+e.currentTarget.dataset.parentid
				})
			}else if(e.currentTarget.dataset.type==14){
				wx.navigateTo({
					url: "../articleDetail/articleDetail?judgeType=&id="+e.currentTarget.dataset.id
				})
			}
		},
  }
})
